﻿using System;
using Hammock;
using Hammock.Streaming;
using Hammock.Web;

using Weibo4Sharp.Model;
using Weibo4Sharp.Extensions;

namespace Weibo4Sharp.Service
{
    partial class WeiboService
    {
        //private readonly RestClient _userStreamingClient;

        ///// <summary>
        ///// Cancels pending streaming actions from this service.
        ///// </summary>
        //public virtual void CancelStreaming()
        //{
        //    if(_userStreamingClient != null)
        //    {
        //        _userStreamingClient.CancelStreaming();
        //    }
        //}


//        /// <summary>
//        /// Accesses an asynchronous Weibo user stream indefinitely, until terminated.
//        /// </summary>
//        /// <seealso href="http://dev.twitter.com/pages/user_streams" />
//        /// <param name="action"></param>
//        /// <returns></returns>
//#if !WINDOWS_PHONE
//        public virtual IAsyncResult StreamUser(Action<WeiboUserStreamArtifact, WeiboResponse> action)
//#else
//        public void StreamUser(Action<WeiboUserStreamArtifact, WeiboResponse> action)
//#endif
//        {
//            var options = new StreamOptions { ResultsPerCallback = 1 };

//#if !WINDOWS_PHONE
//            return 
//#endif
//            WithHammockUserStreaming(options, action, "user.json");
//        }

//#if !WINDOWS_PHONE
//        private IAsyncResult WithHammockUserStreaming<T>(StreamOptions options, Action<T, WeiboResponse> action, string path) where T : class
//#else
//        private void WithHammockUserStreaming<T>(StreamOptions options, Action<T, WeiboResponse> action, string path) where T : class
//#endif
//        {
//            var request = PrepareHammockQuery(path);
//#if !WINDOWS_PHONE
//            return 
//#endif 
//            WithHammockStreamingImpl(_userStreamingClient, request, options, action);
//        }

#if !WINDOWS_PHONE
        private IAsyncResult WithHammockStreamingImpl<T>(RestClient client, RestRequest request, StreamOptions options, Action<T, WeiboResponse> action)
#else
        private static void WithHammockStreamingImpl<T>(RestClient client, RestRequest request, StreamOptions options, Action<T, WeiboResponse> action)
#endif
        {
            request.StreamOptions = options;
            request.Method = WebMethod.Get;
#if SILVERLIGHT
            request.AddHeader("X-User-Agent", client.UserAgent); 
#endif
            
#if !WINDOWS_PHONE
            return
#endif
            client.BeginRequest(request, new RestCallback<T>((req, resp, state) =>
            {
                Exception exception;
                var entity = TryAsyncResponse(() => 
                        {
#if !SILVERLIGHT
                            SetResponse(resp);
#endif
                            return resp.ContentEntity;
                        },
                        out exception);
                action(entity, new WeiboResponse(resp, exception));
            }));
        }
    }
}
